﻿package com.tvein.sanguo.swc.action.login
{
	//flash
	import flash.display.MovieClip;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.events.KeyboardEvent;
	import flash.display.StageAlign;
	import flash.display.StageScaleMode;
	import fl.controls.TextInput;
	//A3D
	//custom
	import com.tvein.sanguo.swc.common.SwcEvent;
	import com.tvein.sanguo.swc.common.MP3Click;
//
	public class LoginEnter extends MovieClip
	{
		public static const NAME:String = "Login-Enter";
		
		private var effectVol:int = 100;
		
		private var txtAccount:TextInput;
		private var txtPassword:TextInput;
		
		public function LoginEnter()
		{
			stage?init(null):addEventListener(Event.ADDED_TO_STAGE, init);
		}
		
		private function init(evt:Event):void
		{
			removeEventListener(Event.ADDED_TO_STAGE , init);
			addEventListener(Event.REMOVED_FROM_STAGE, _onRemoved);
			stage.addEventListener(KeyboardEvent.KEY_UP, _keyDownHandler);
			stage.addEventListener(Event.RESIZE, _resizeUI);
			
			stage.scaleMode = StageScaleMode.NO_SCALE;
			stage.align = StageAlign.TOP_LEFT;
			
			txtAccount = new TextInput();
			txtAccount.x = 20;
			txtAccount.y = 80;
			txtAccount.width = 200;
			txtAccount.height = 22;
			this.addChild(txtAccount);
			
			txtPassword = new TextInput();
			txtPassword.x = 20;
			txtPassword.y = 160;
			txtPassword.width = 200;
			txtPassword.height = 22;
			txtPassword.displayAsPassword = true;
			this.addChild(txtPassword);				
			
			txtAccount.tabIndex = 1;
			txtPassword.tabIndex = 2;
			btnSend.tabIndex = 3;
			btnCreate.tabIndex = 4;
			btnForget.tabIndex = 5;
			
			btnSend.visible = false;
			
			btnSend.addEventListener(MouseEvent.CLICK, onClick);
			btnForget.addEventListener(MouseEvent.CLICK, onClick);
			btnCreate.addEventListener(MouseEvent.CLICK, onClick);
			
			mcFrame.reSize(240, 300);
			
			_resizeUI();
		}

		public function console(Type:String, params:Object):void
		{
			switch (Type)
			{
				case "setEffect":
					effectVol = params.Volume;
					break;
			}
		}

		private function _onRemoved(evt:Event):void
		{
			removeEventListener(Event.REMOVED_FROM_STAGE, _onRemoved);
			stage.removeEventListener(KeyboardEvent.KEY_UP, _keyDownHandler);
			stage.removeEventListener(Event.RESIZE, _resizeUI);
			
			btnSend.removeEventListener(MouseEvent.CLICK, onClick);
			btnForget.removeEventListener(MouseEvent.CLICK, onClick);
			btnCreate.removeEventListener(MouseEvent.CLICK, onClick);
		}

		private function _keyDownHandler(evt:KeyboardEvent):void
		{
			if (txtAccount.length > 0 && txtPassword.length > 0)
			{
				btnSend.visible = true;
			}
			
			if (txtAccount.length == 0 || txtPassword.length == 0)
			{
				btnSend.visible = false;
			}
			
			
			if (evt.keyCode == 13 && btnSend.visible)
			{
				var mp3Click:MP3Click = new MP3Click();
				mp3Click.playback(effectVol * .01);
				
				dispatchEvent(new SwcEvent(NAME, ["-userLogin", [txtAccount.text, txtPassword.text]]));
			}
		}

		private function _resizeUI(evt:Event = null):void
		{
			this.x = (stage.stageWidth - this.width) * .5
			if ((stage.stageHeight - this.height) > 100)
			{
				this.y = (stage.stageHeight - this.height) * .5;
			}
			else
			{
				this.y = 50;
			}
		}
		
		private function onClick(evt:MouseEvent):void
		{			
			var mp3Click:MP3Click = new MP3Click();
			mp3Click.playback(effectVol * .01);
			
			switch(evt.target.name)
			{
				case "btnSend":
					btnSend.visible = false;
					btnForget.visible = false;
					btnCreate.visible = false;
					dispatchEvent(new SwcEvent(NAME, ["-userLogin", [txtAccount.text, txtPassword.text]]));
					break;
				case "btnForget":
					dispatchEvent(new SwcEvent(NAME, ["-showBox", "forget"]));
					break;
				case "btnCreate":
					dispatchEvent(new SwcEvent(NAME, ["-showBox", "create"]));
					break;
			}
		}
	}
}